package com.kk.base.controller.admin;

import com.kk.base.bean.CodeMsg;
import com.kk.base.bean.PageBean;
import com.kk.base.bean.Result;
import com.kk.base.entity.admin.BuyOff;
import com.kk.base.entity.admin.ProcessRecords;
import com.kk.base.entity.admin.User;
import com.kk.base.entity.admin.UserRoleTypeEnum;
import com.kk.base.service.admin.BuyOffService;
import com.kk.base.service.admin.UserService;
import com.kk.base.util.SessionUtil;
import com.kk.base.util.ValidateEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin/buyOff")
public class BuyOffController {

    @Autowired
    private BuyOffService buyOffService;

    @Autowired
    private UserService userService;

    /**
     * 发货列表根据订单编号查询
     * @param model
     * @param waybillNumber
     * @param pageBean
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model,String waybillNumber, PageBean<BuyOff> pageBean){
        model.addAttribute("waybillNumber",waybillNumber);
        model.addAttribute("users",userService.findByUserTypeNot());
        model.addAttribute("title", "发货列表");
        model.addAttribute("pageBean",buyOffService.findList(pageBean,waybillNumber));
        return "admin/buy_off/list";
    }

    /**
     * 指定派送列表根据订单编号查询
     * @param model
     * @param waybillNumber
     * @param pageBean
     * @return
     */
    @RequestMapping("/deliveryList")
    public String findDeliveryList(Model model,String waybillNumber, PageBean<BuyOff> pageBean){
        User loginedUser = SessionUtil.getLoginedUser();
        model.addAttribute("waybillNumber",waybillNumber);
        model.addAttribute("users",userService.findByUserTypeAndBranchIdAndStatus(UserRoleTypeEnum.COURIER,loginedUser.getId(),User.ADMIN_USER_STATUS_ENABLE));
        model.addAttribute("title", "指定派送列表");
        model.addAttribute("pageBean",buyOffService.findDeliveryList(pageBean,waybillNumber));
        return "admin/buy_off/delivery_list";
    }

    /**
     * 快递员派送列表根据订单编号拆线呢
     * @param model
     * @param waybillNumber
     * @param pageBean
     * @return
     */
    @RequestMapping("/signForList")
    public String signForList(Model model,String waybillNumber, PageBean<BuyOff> pageBean){
        model.addAttribute("waybillNumber",waybillNumber);
        model.addAttribute("title", "派送列表");
        model.addAttribute("pageBean",buyOffService.signForList(pageBean,waybillNumber));
        return "admin/buy_off/sign_for_list";
    }


    /**
     * 指定快递员派送
     * @param id
     * @param userId
     * @return
     */
    @RequestMapping("/specifyTheDelivery")
    @ResponseBody
    public Result<Boolean> specifyTheDelivery(@RequestParam("id") Long id,
                                              @RequestParam("userId")Long userId){
        BuyOff buyOff = buyOffService.find(id);
        BuyOff newBuyOff = new BuyOff();
        newBuyOff.setStatus(BuyOff.DESIGNATE_DISPATCHERS);
        newBuyOff.setBuyOffUser(userService.find(userId));
        newBuyOff.setBuyOffExpressMailOrder(buyOff.getBuyOffExpressMailOrder());
        buyOff.setOperation(ProcessRecords.HAVE_HANDLED);
        CodeMsg validate = ValidateEntityUtil.validate(buyOff);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        BuyOff Newsave = buyOffService.save(newBuyOff);
        if (null == Newsave) {
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER1_BUYOFF_EDIT_ERROR);
        }
        BuyOff save = buyOffService.save(buyOff);
        if (null == save) {
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER1_BUYOFF_EDIT_ERROR);
        }


        return Result.success(true);
    }

    /**
     * 快递员签收
     * @param id
     * @return
     */
    @RequestMapping("/signFor")
    @ResponseBody
    public Result<Boolean> signFor(@RequestParam("id") Long id){

        if (null == id){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER1_BUYOFF1_EDIT_ERROR);

        }

        if (1 == buyOffService.signForComplete(id)) {
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER1_BUYOFF1_EDIT_ERROR);
        }

        return Result.success(true);
    }

}
